//
//  XXPlayBottomView.m
//  you
//
//  Created by tom on 16/5/30.
//  Copyright © 2016年 seven. All rights reserved.
//

#import "XXPlayBottomView.h"

#define LabelWidth 40 

@interface XXPlayBottomView ()

@property(nonatomic,weak) UILabel  *currentLabel  ;

@property(nonatomic,weak) UILabel  *totalLabel;

@property(nonatomic,weak) UISlider *slider ;

@property(nonatomic,weak) UIButton *fullScreenBtn ;


@end

@implementation XXPlayBottomView

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor blackColor];
        
        UILabel *currentLabel = [UILabel new] ;
        currentLabel.font = [UIFont systemFontOfSize:12];
        currentLabel.textColor = [UIColor whiteColor];
        currentLabel.textAlignment = NSTextAlignmentCenter ;
        currentLabel.backgroundColor = [UIColor blueColor];
        [self addSubview:currentLabel];
        _currentLabel = currentLabel ;
        _currentLabel.text = @"00:00";
        
        UILabel *totalLabel = [UILabel new];
        totalLabel.font = [UIFont systemFontOfSize:12];
        totalLabel.textColor = currentLabel.textColor ;
        totalLabel.textAlignment = NSTextAlignmentCenter ;
        [self addSubview:totalLabel];
        totalLabel.backgroundColor = [UIColor blueColor];

        _totalLabel = totalLabel;
        _totalLabel.text = @"00:00";
        
        
        UISlider *slider = [[UISlider alloc]init];
        [slider setThumbImage:[UIImage imageNamed:@"slider"] forState:UIControlStateNormal];
        slider.minimumTrackTintColor = [UIColor whiteColor];
        slider.maximumTrackTintColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.6];
        [slider addTarget:self action:@selector(clickSlider:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:slider];
        _slider = slider;
        
        UIButton *fullScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [fullScreenBtn setImage:[UIImage imageNamed:@"resize_3"] forState:UIControlStateNormal];
        fullScreenBtn.tag = 1 ;
        [fullScreenBtn addTarget:self action:@selector(fullScreen) forControlEvents:UIControlEventTouchUpInside];
        fullScreenBtn.layer.borderColor = [UIColor blackColor].CGColor ;
        fullScreenBtn.layer.borderWidth = 1 ;
        [self addSubview:fullScreenBtn];
        _fullScreenBtn = fullScreenBtn ;
        
    }
    return self;
}
- (void) clickSlider:(UISlider *) slider {
    NSLog(@"slider.value=%f",slider.value);
    if ([self.delegate respondsToSelector:@selector(seektoTime:)]) {
        [self.delegate seektoTime:slider.value];
    }

}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    _currentLabel.frame = CGRectMake(5, 0, LabelWidth, CGRectGetHeight(self.bounds));
   
    _fullScreenBtn.frame = CGRectMake(CGRectGetWidth(self.bounds)-32-5, 0, 32, CGRectGetHeight(self.bounds));
    NSLog(@"fullScreenBtn %@",NSStringFromCGRect(_fullScreenBtn.frame));
    
    _totalLabel.frame = CGRectMake(CGRectGetWidth(self.bounds) - CGRectGetWidth(_fullScreenBtn.frame) - 10 - LabelWidth , 0, LabelWidth , CGRectGetHeight(self.bounds));
 
    _slider.frame = CGRectMake(CGRectGetMaxX(_currentLabel.frame)+5, 0, CGRectGetWidth(self.bounds) - CGRectGetWidth(_currentLabel.frame) - CGRectGetWidth(_totalLabel.frame)-CGRectGetWidth(_fullScreenBtn.frame)- 25  , CGRectGetHeight(self.bounds));
}

- (void)setCurrentTime:(float)currentTime {
    _currentTime = currentTime ;
    int  minutes = currentTime / 60 ;
    int  second =  currentTime - minutes * 60 ;
    
    NSLog(@"当前 =%@,%@:%@",@(currentTime),@(minutes),@(second));
    self.currentLabel.text = [NSString stringWithFormat:@"%02d:%02d",minutes,second];
    
}

- (void)setTotalTime:(float)totalTime {
    _totalTime = totalTime ;
    int minutes = totalTime / 60 ;
    int second = totalTime - minutes * 60 ;
    self.totalLabel.text = [NSString stringWithFormat:@"%02d:%02d",minutes,second];
    NSLog(@"总 =%@",@(totalTime));
}

- (void)setSlideValue:(float)slideValue {
    _slideValue = slideValue ;
    self.slider.value = slideValue ;
    
}
- (void)fullScreen {
    NSLog(@"全屏");
    if (_fullScreenBtn.tag == 1  ) {
        _fullScreenBtn.tag = 0 ;
        [self.delegate clickFullScreenBtn:UIInterfaceOrientationLandscapeRight];
    }else if (_fullScreenBtn.tag == 0 ){
        _fullScreenBtn.tag = 1 ;
        [self.delegate clickFullScreenBtn:UIInterfaceOrientationPortrait];
    }
}


@end
